﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 , gt TECH, Ltd.
//--------------------------------------------------------------------

using System;
using System.Data;

namespace UMPlatForm.Business
{
  using UMPlatForm.Business;
  

 /// <summary>
 /// PiPermissionItemManager
 /// 操作权限项定义
 ///
 /// 修改纪录
 ///
 ///		2012-03-02 版本：1.0 EricHu 创建主键。
 ///
 /// 版本：1.0
 ///
 /// <author>
 ///		<name>EricHu</name>
 ///		<date>2012-03-02</date>
 /// </author>
 /// </summary>
 public partial class PiPermissionItemManager : BaseManager, IBaseManager
 {
  /// <summary>
  /// 构造函数
  /// </summary>
  public PiPermissionItemManager()
  {
      base.CurrentTableName = PiPermissionItemTable.TableName;
      base.PrimaryKey = "Id";
  }

  /// <summary>
  /// 构造函数
  /// <param name="tableName">指定表名</param>
  /// </summary>
  public PiPermissionItemManager(string tableName)
  {
      base.CurrentTableName = tableName;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="dbHelper">数据库连接</param>
  public PiPermissionItemManager(IDbHelper dbHelper): this()
  {
      DbHelper = dbHelper;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="userInfo">用户信息</param>
  public PiPermissionItemManager(UserInfo userInfo) : this()
  {
      UserInfo = userInfo;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="dbHelper">数据库连接</param>
  /// <param name="userInfo">用户信息</param>
  public PiPermissionItemManager(IDbHelper dbHelper, UserInfo userInfo) : this(dbHelper)
  {
      UserInfo = userInfo;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="dbHelper">数据库连接</param>
  /// <param name="userInfo">用户信息</param>
  /// <param name="tableName">指定表名</param>
  public PiPermissionItemManager(IDbHelper dbHelper, UserInfo userInfo, string tableName) : this(dbHelper, userInfo)
  {
      base.CurrentTableName = tableName;
  }

  /// <summary>
  /// 添加
  /// </summary>
  /// <param name="piPermissionItemEntity">实体</param>
  /// <returns>主键</returns>
  public string Add(PiPermissionItemEntity piPermissionItemEntity)
  {
      return this.AddEntity(piPermissionItemEntity);
  }

  /// <summary>
  /// 添加
  /// </summary>
  /// <param name="piPermissionItemEntity">实体</param>
  /// <param name="identity">自增量方式</param>
  /// <param name="returnId">返回主键</param>
  /// <returns>主键</returns>
  public string Add(PiPermissionItemEntity piPermissionItemEntity, bool identity, bool returnId)
  {
      this.Identity = identity;
      this.ReturnId = returnId;
      return this.AddEntity(piPermissionItemEntity);
  }

  /// <summary>
  /// 更新
  /// </summary>
  /// <param name="piPermissionItemEntity">实体</param>
  public int Update(PiPermissionItemEntity piPermissionItemEntity)
  {
      return this.UpdateEntity(piPermissionItemEntity);
  }

  /// <summary>
  /// 获取实体
  /// </summary>
  /// <param name="id">主键</param>
  public PiPermissionItemEntity GetEntity(string id)
  {
      return GetEntity(int.Parse(id));
  }

  public PiPermissionItemEntity GetEntity(int id)
  {
      PiPermissionItemEntity piPermissionItemEntity = new PiPermissionItemEntity(this.GetDT(PiPermissionItemTable.FieldId, id));
      return piPermissionItemEntity;
  }

  /// <summary>
  /// 添加实体
  /// </summary>
  /// <param name="piPermissionItemEntity">实体</param>
  public string AddEntity(PiPermissionItemEntity piPermissionItemEntity)
  {
       string sequence = string.Empty;
      if (piPermissionItemEntity.SortCode == null || piPermissionItemEntity.SortCode == 0)
      {
          BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper, this.Identity);
          sequence = sequenceManager.GetSequence(this.CurrentTableName);
          piPermissionItemEntity.SortCode = int.Parse(sequence);
      }
      SQLBuilder sqlBuilder = new SQLBuilder(DbHelper, this.Identity, this.ReturnId);
      sqlBuilder.BeginInsert(this.CurrentTableName, PiPermissionItemTable.FieldId);
  if (!this.Identity) 
  {
      sqlBuilder.SetValue(PiPermissionItemTable.FieldId, piPermissionItemEntity.Id);
  }
  else
  {
  if (!this.ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))
  {
      if (DbHelper.CurrentDbType == CurrentDbType.Oracle)
      {
          sqlBuilder.SetFormula(PiPermissionItemTable.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
      }
      if (DbHelper.CurrentDbType == CurrentDbType.DB2)
      {
          sqlBuilder.SetFormula(PiPermissionItemTable.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
      }
  }
  else
  {
      if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))
      {
if (piPermissionItemEntity.Id == null)
{
    if (string.IsNullOrEmpty(sequence))
    {
        BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper, this.Identity);
        sequence = sequenceManager.GetSequence(this.CurrentTableName);
    }
    piPermissionItemEntity.Id = int.Parse(sequence);
}
        sqlBuilder.SetValue(PiPermissionItemTable.FieldId, piPermissionItemEntity.Id);
      }
  }
  }
      this.SetEntity(sqlBuilder, piPermissionItemEntity);
      if (UserInfo != null) 
      { 
      sqlBuilder.SetValue(PiPermissionItemTable.FieldCreateUserId, UserInfo.Id);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldCreateBy, UserInfo.RealName);
      } 
      sqlBuilder.SetDBNow(PiPermissionItemTable.FieldCreateOn);
      if (UserInfo != null) 
      { 
      sqlBuilder.SetValue(PiPermissionItemTable.FieldModifiedBy, UserInfo.RealName);
      } 
      sqlBuilder.SetDBNow(PiPermissionItemTable.FieldModifiedOn);
    if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access))
    {
        sequence = sqlBuilder.EndInsert().ToString();
    }
    else
    {
        sqlBuilder.EndInsert();
    }
    return sequence;
  }

  /// <summary>
  /// 更新实体
  /// </summary>
  /// <param name="piPermissionItemEntity">实体</param>
  public int UpdateEntity(PiPermissionItemEntity piPermissionItemEntity)
  {
      SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);
      sqlBuilder.BeginUpdate(this.CurrentTableName);
      this.SetEntity(sqlBuilder, piPermissionItemEntity);
      if (UserInfo != null) 
      { 
      sqlBuilder.SetValue(PiPermissionItemTable.FieldModifiedBy, UserInfo.RealName);
      } 
      sqlBuilder.SetDBNow(PiPermissionItemTable.FieldModifiedOn);
      sqlBuilder.SetWhere(PiPermissionItemTable.FieldId, piPermissionItemEntity.Id);
      return sqlBuilder.EndUpdate();
  }

  /// <summary>
  /// 设置实体
  /// </summary>
  /// <param name="piPermissionItemEntity">实体</param>
  private void SetEntity(SQLBuilder sqlBuilder, PiPermissionItemEntity piPermissionItemEntity)
  {
      sqlBuilder.SetValue(PiPermissionItemTable.FieldParentId, piPermissionItemEntity.ParentId);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldCode, piPermissionItemEntity.Code);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldFullName, piPermissionItemEntity.FullName);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldCategoryCode, piPermissionItemEntity.CategoryCode);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldIsScope, piPermissionItemEntity.IsScope);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldIsPublic, piPermissionItemEntity.IsPublic);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldAllowEdit, piPermissionItemEntity.AllowEdit);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldAllowDelete, piPermissionItemEntity.AllowDelete);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldLastCall, piPermissionItemEntity.LastCall);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldSortCode, piPermissionItemEntity.SortCode);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldDescription, piPermissionItemEntity.Description);
      sqlBuilder.SetValue(PiPermissionItemTable.FieldModifyUserId, piPermissionItemEntity.ModifyUserId);
  }

  /// <summary>
  /// 删除实体
  /// </summary>
  /// <param name="id">主键</param>
  /// <returns>影响行数</returns>
  public int Delete(int id)
  {
      return this.Delete(PiPermissionItemTable.FieldId, id);
  }
  }
}
